 package program.TokenAndParser;

import java.util.ArrayList;
import java.util.Stack;
import program.Atom;
import program.SExprList;
import program.SExpression;

/**
 * A class representing the parser.
 * 
 * @author Jef Keijers/Bachelor of Science: Informatics
 * @version 1.0
 */
public class Parser {
	
	/**
	 * This method parses the tokenlist into a SExpression
	 * @param tokenlist
	 */
	public SExpression parse(ArrayList <Token> tokenlist) {
		Stack<SExprList> previouslists=new Stack<SExprList>(); 
		SExprList last=new SExprList();; 
		previouslists.push(last);
		for (int i=0; i<tokenlist.size();i++) {   
			Token token=tokenlist.get(i);
			if (token.getToken().equals("(")) {		
				previouslists.push(new SExprList());
			} else if (token.getToken().equals(")")) { 
				last=previouslists.pop(); 
				previouslists.peek().add(last);
			} else {
				previouslists.peek().add(new Atom(token.getToken()));
			}		 
		}
		return previouslists.pop().getList().get(0);

	}
}